from typing import List
import os
import sys


project_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.insert(0, project_root)

from src.backend.parsers.parse_file_tree import FileTreeNode
from src.backend.parsers.parse_file_detail import LsLaOutput

from src.core.result_collector import TaskResult


class DataBridge:
    def __init__(self):
        pass
    def get_file_tree(self, base_path: str, find_output: str) -> dict:
        root = FileTreeNode.build_file_tree_from_find_output(base_path, find_output)
        json_data = root.parse_json()

        return json_data
    
    def parse_to_frontend(self, request_data:dict,task_results: List[TaskResult]) -> dict:

        if task_results is None:
            return dict()
        
        frontend_json = dict()
        if  task_results[0].task_name == 'read' or \
            task_results[0].task_name == 'write':

            task_result = task_results[0]
            frontend_json['cmd_event'] = task_result.task_name
            frontend_json['msg'] = '任务执行成功' if task_result.success else '任务执行失败: '+task_result.command_results[0].stderr
            frontend_json['success'] = task_result.success
            frontend_json['data'] = []
            for item in task_result.command_results:
                frontend_json['data'].append({
                    'success': item.success,
                    'content': item.stdout,
                    'base_path': request_data['params'][0],
                })
            return frontend_json
        elif task_results[0].task_name == 'tree':
            task_result = task_results[0]
            frontend_json['cmd_event'] = task_result.task_name
            frontend_json['msg'] = '任务执行成功' if task_result.success else '任务执行失败: '+task_result.command_results[0].stderr
            frontend_json['success'] = task_result.success
            frontend_json['data'] = []
            for item in task_result.command_results:
                # 转换为LayUI树组件的数据结构
                base_path = request_data['params'][0]
                root = FileTreeNode.build_file_tree_from_find_output(base_path, item.stdout)
                tree_json = root.parse_json()

                frontend_json['data'].append({
                    'success': item.success,
                    'content': tree_json['children'],
                    'base_path':root.abs_path,
                })
            return frontend_json
        
        elif task_results[0].task_name == 'detail':
            task_result = task_results[0]
            frontend_json['cmd_event'] = task_result.task_name
            frontend_json['msg'] = '任务执行成功' if task_result.success else '任务执行失败: '+task_result.command_results[0].stderr
            frontend_json['success'] = task_result.success
            frontend_json['data'] = []
            for item in task_result.command_results:
                # 转换为LayUI表格组件的数据结构
                ls_la_output = LsLaOutput.parse(item.stdout)
                table_json = ls_la_output.parse_json()
                frontend_json['data'].append({
                    'success': item.success,
                    'content': table_json['entries'],
                    'bash_path': table_json['bash_path'],
                })
            return frontend_json
        elif task_results[0].task_name == 'rename':
            task_result = task_results[0]
            frontend_json['cmd_event'] = task_result.task_name
            frontend_json['msg'] = '任务执行成功' if task_result.success else '任务执行失败: '+task_result.command_results[0].stderr
            frontend_json['success'] = task_result.success
            frontend_json['data'] = []
            return frontend_json
        elif task_results[0].task_name == 'mkdir':
            task_result = task_results[0]
            frontend_json['cmd_event'] = task_result.task_name
            frontend_json['msg'] = '任务执行成功' if task_result.success else '任务执行失败: '+task_result.command_results[0].stderr
            frontend_json['success'] = task_result.success
            frontend_json['data'] = []
            return frontend_json
        elif task_results[0].task_name == 'touch':
            task_result = task_results[0]
            frontend_json['cmd_event'] = task_result.task_name
            frontend_json['msg'] = '任务执行成功' if task_result.success else '任务执行失败: '+task_result.command_results[0].stderr
            frontend_json['success'] = task_result.success
            frontend_json['data'] = []
            return frontend_json
        elif task_results[0].task_name == 'delete':
            task_result = task_results[0]
            frontend_json['cmd_event'] = task_result.task_name
            frontend_json['msg'] = '任务执行成功' if task_result.success else '任务执行失败: '+task_result.command_results[0].stderr
            frontend_json['success'] = task_result.success
            frontend_json['data'] = []
            return frontend_json
        elif task_results[0].task_name == 'move':
            task_result = task_results[0]
            frontend_json['cmd_event'] = task_result.task_name
            frontend_json['msg'] = '任务执行成功' if task_result.success else '任务执行失败: '+task_result.command_results[0].stderr
            frontend_json['success'] = task_result.success
            frontend_json['data'] = []
            return frontend_json
        elif task_results[0].task_name == 'copy':
            task_result = task_results[0]
            frontend_json['cmd_event'] = task_result.task_name
            frontend_json['msg'] = '任务执行成功' if task_result.success else '任务执行失败: '+task_result.command_results[0].stderr
            frontend_json['success'] = task_result.success
            frontend_json['data'] = []
            return frontend_json
        elif task_results[0].task_name == 'chmod':
            task_result = task_results[0]
            frontend_json['cmd_event'] = task_result.task_name
            frontend_json['msg'] = '任务执行成功' if task_result.success else '任务执行失败: '+task_result.command_results[0].stderr
            frontend_json['success'] = task_result.success
            frontend_json['data'] = []
            return frontend_json

        
        return frontend_json
        
    
       




